package com.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.beans.Movie;
import com.bussiness.MovieBussinessProxy;
import com.constants.ServiceEndPoints;
import com.constants.UIMessages;

/**
 * Servlet implementation class CreateMovieServlet
 */
public class CreateMovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MovieBussinessProxy movieBI = new MovieBussinessProxy();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CreateMovieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Movie addMovie = new Movie();
		String[] t;
		try
		{
			HttpSession session = request.getSession(true);
			RequestDispatcher dispatcher;
			movieBI.setEndpoint(ServiceEndPoints.MovieEndPoint);
			addMovie.setMovieName(request.getParameter("movieName").toString());
			addMovie.setMovieBanner(request.getParameter("movieBanner").toString());
			addMovie.setCategoryid(Integer.parseInt(request.getParameter("category")));
			addMovie.setTotalCopy(Integer.parseInt(request.getParameter("copies")));
			addMovie.setAvailableCopy(Integer.parseInt(request.getParameter("copies")));
			addMovie.setRent(Integer.parseInt(request.getParameter("rent")));
			t = request.getParameter("dateText").toString().split("-");
			addMovie.setReleaseDate(t[2]+t[1]+t[0]);
			addMovie.setRating(0.0);
			addMovie.setNo_of_ratings(0);
			movieBI.createMovie(addMovie);
			
			request.setAttribute("ServerMessage","Movie created successfully");	 
			
			dispatcher = getServletContext().getRequestDispatcher("/createmovie.jsp");
			dispatcher.forward(request, response);
			
		}
		catch(Exception e)
		{
			request.setAttribute("ServerMessage", UIMessages.SERVICE_UNAVAILABLE);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/createmovie.jsp");
			dispatcher.forward(request, response);
		}
	}

}
